package com.shop2cn.iapi.shenqisettlement.testcase;

import java.math.BigDecimal;
import java.util.List;

import com.shop2cn.iapi.shenqisettlement.parameter.SaveGoodsOwnerPaymentReq;
import com.shop2cn.iapi.shenqisettlement.service.SaveGoodsOwnerPaymentCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.shenqisettlement.GoodsOwnerPayment;
import com.ymttest.database.model.shenqisettlement.GoodsOwnerSettlementBill;
import com.ymttest.database.sqlwapper.shenqisettlement.GoodsOwnerPaymentWrapper;
import com.ymttest.database.sqlwapper.shenqisettlement.GoodsOwnerSettlementBillWrapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;

import org.junit.*;
import org.junit.experimental.categories.Category;

/**
 * 根据国家获取汇率 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SaveGoodsOwnerPayment {
	
	private static SaveGoodsOwnerPaymentReq saveGoodsOwnerPaymentReq;
	private static SaveGoodsOwnerPaymentCall saveGoodsOwnerPaymentCall;
	
	GoodsOwnerSettlementBillWrapper goodsOwnerSettlementBillWrapper = new GoodsOwnerSettlementBillWrapper();
	GoodsOwnerPaymentWrapper goodsOwnerPaymentWrapper = new GoodsOwnerPaymentWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("货头线下支付");
	}

	@Before
	public void caseUp() {
		saveGoodsOwnerPaymentReq = new SaveGoodsOwnerPaymentReq();
		saveGoodsOwnerPaymentCall = new SaveGoodsOwnerPaymentCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(saveGoodsOwnerPaymentCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verify() {

		Logger.comment("验证goods_owner_settlement_bill表数据");
		
		GoodsOwnerSettlementBill actualGoodsOwnerSettlementBill = goodsOwnerSettlementBillWrapper
				.selectByPrimaryKey(saveGoodsOwnerPaymentReq.getBillId());
		
		List<GoodsOwnerPayment> actualGoodsOwnerPayments = goodsOwnerPaymentWrapper
				.selectByBillId(saveGoodsOwnerPaymentReq.getBillId());
		
		BigDecimal totalPaidAmount = actualGoodsOwnerPayments.stream()
				.map(payment -> payment.getAmount())
				.reduce(BigDecimal.ZERO, BigDecimal::add);
		
		int paymentStatus = 0;

		if (totalPaidAmount.compareTo(actualGoodsOwnerSettlementBill
				.getSettlement_amount()) == 0)
			paymentStatus = 2;
		else
			paymentStatus = 1;

		DataUtil.verifyDecimalEquals(totalPaidAmount,
				actualGoodsOwnerSettlementBill.getPaid_settlement_amount(),
				"验证Paid_settlement_amount");
		Logger.verifyEquals(paymentStatus,
				actualGoodsOwnerSettlementBill.getPayment_status(),
				"验证Payment_status");

		Logger.comment("验证goods_owner_payment表数据");
		
		GoodsOwnerPayment actualGoodsOwnerPayment = actualGoodsOwnerPayments.get(0);
		
		Logger.verifyEquals(actualGoodsOwnerSettlementBill.getMerchant_id(),
				actualGoodsOwnerPayment.getMerchant_id(), "验证Merchant_id");
		Logger.verifyEquals(actualGoodsOwnerSettlementBill.getGoods_owner_id(),
				actualGoodsOwnerPayment.getGoods_owner_id(), "验证Goods_owner_id");
		Logger.verifyEquals(
				actualGoodsOwnerSettlementBill.getSettlement_currency(),
				actualGoodsOwnerPayment.getCurrency(), "验证Currency");
		Logger.verifyEquals(saveGoodsOwnerPaymentReq.getPaymentId(),
				actualGoodsOwnerPayment.getPayment_id(), "验证Payment_id");
		DataUtil.verifyDecimalEquals(saveGoodsOwnerPaymentReq.getAmount(),
				actualGoodsOwnerPayment.getAmount(), "验证Amount");
		Logger.verifyEquals(saveGoodsOwnerPaymentReq.getRemark(),
				actualGoodsOwnerPayment.getRemark(), "验证Remark");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SaveGoodsOwnerPayment_001() {
		Logger.start(true, "线下打款，全款支付");
		try {
			
			Logger.comment("数据准备");
			
			// 更新账单数据为未支付
			goodsOwnerSettlementBillWrapper.updateBillToNotPay(8L);
			
			goodsOwnerPaymentWrapper.deleteByMerchantId(500011795L);
			
			Logger.comment("数据准备结束");
			
			saveGoodsOwnerPaymentReq.setAmount(new BigDecimal("100.00"));
			saveGoodsOwnerPaymentReq.setBillId(8L);
			saveGoodsOwnerPaymentReq.setPaymentId(TestDataManager.randomStr(8));
			saveGoodsOwnerPaymentReq.setRemark("remark");
			
			saveGoodsOwnerPaymentCall.setData(saveGoodsOwnerPaymentReq);
			saveGoodsOwnerPaymentCall.callService();

			this.verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SaveGoodsOwnerPayment_002() {
		Logger.start(true, "线下打款，部分支付");
		try {
			
			Logger.comment("数据准备");
			
			// 更新账单数据为未支付
			goodsOwnerSettlementBillWrapper.updateBillToNotPay(8L);
			
			goodsOwnerPaymentWrapper.deleteByMerchantId(500011795L);
			
			Logger.comment("数据准备结束");
			
			saveGoodsOwnerPaymentReq.setAmount(BigDecimal.ONE);
			saveGoodsOwnerPaymentReq.setBillId(8L);
			saveGoodsOwnerPaymentReq.setPaymentId(TestDataManager.randomStr(8));
			saveGoodsOwnerPaymentReq.setRemark("remark");
			
			saveGoodsOwnerPaymentCall.setData(saveGoodsOwnerPaymentReq);
			saveGoodsOwnerPaymentCall.callService();

			this.verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SaveGoodsOwnerPayment_003() {
		Logger.start(true, "线下打款，多次支付");
		try {
			
			Logger.comment("数据准备");
			
			// 更新账单数据为未支付
			goodsOwnerSettlementBillWrapper.updateBillToNotPay(8L);
			
			// 删除支付记录
			goodsOwnerPaymentWrapper.deleteByMerchantId(500011795L);
			
			Logger.comment("数据准备结束");
			
			saveGoodsOwnerPaymentReq.setAmount(BigDecimal.ONE);
			saveGoodsOwnerPaymentReq.setBillId(8L);
			saveGoodsOwnerPaymentReq.setPaymentId(TestDataManager.randomStr(8));
			saveGoodsOwnerPaymentReq.setRemark("remark");
			
			saveGoodsOwnerPaymentCall.setData(saveGoodsOwnerPaymentReq);
			saveGoodsOwnerPaymentCall.callService();
			
			saveGoodsOwnerPaymentReq.setAmount(new BigDecimal("99.00"));
			saveGoodsOwnerPaymentReq.setBillId(8L);
			saveGoodsOwnerPaymentReq.setPaymentId(TestDataManager.randomStr(8));
			saveGoodsOwnerPaymentReq.setRemark("remark");
			
			saveGoodsOwnerPaymentCall.setData(saveGoodsOwnerPaymentReq);
			saveGoodsOwnerPaymentCall.callService();

			this.verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SaveGoodsOwnerPayment_004() {
		Logger.start(true, "线下打款，同一个支付id，多次支付，支付失败");
		try {
			
			Logger.comment("数据准备");
			
			// 更新账单数据为未支付
			goodsOwnerSettlementBillWrapper.updateBillToNotPay(8L);
			
			goodsOwnerPaymentWrapper.deleteByMerchantId(500011795L);
			
			Logger.comment("数据准备结束");
			
			saveGoodsOwnerPaymentReq.setAmount(BigDecimal.TEN);
			saveGoodsOwnerPaymentReq.setBillId(8L);
			saveGoodsOwnerPaymentReq.setPaymentId(TestDataManager.randomStr(8));
			saveGoodsOwnerPaymentReq.setRemark("remark");
			
			saveGoodsOwnerPaymentCall.setData(saveGoodsOwnerPaymentReq);
			saveGoodsOwnerPaymentCall.callService();

			saveGoodsOwnerPaymentCall.callService();
			
			Logger.verifyEquals(3, saveGoodsOwnerPaymentCall.getRetultCode(),
					"验证RetultCode");
			Logger.verifyEquals("重复支付,不能进行支付操作.",
					saveGoodsOwnerPaymentCall.getResultMessage(),
					"验证RetultCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SaveGoodsOwnerPayment_005() {
		Logger.start(true, "线下打款，一次支付，支付金额超过结算金额，支付失败");
		try {
			
			Logger.comment("数据准备");
			
			// 更新账单数据为未支付
			goodsOwnerSettlementBillWrapper.updateBillToNotPay(8L);
			
			goodsOwnerPaymentWrapper.deleteByMerchantId(500011795L);
			
			Logger.comment("数据准备结束");
			
			saveGoodsOwnerPaymentReq.setAmount(new BigDecimal("100.01"));
			saveGoodsOwnerPaymentReq.setBillId(8L);
			saveGoodsOwnerPaymentReq.setPaymentId(TestDataManager.randomStr(8));
			saveGoodsOwnerPaymentReq.setRemark("remark");
			
			saveGoodsOwnerPaymentCall.setData(saveGoodsOwnerPaymentReq);
			saveGoodsOwnerPaymentCall.callService();
			
			Logger.verifyEquals(3, saveGoodsOwnerPaymentCall.getRetultCode(),
					"验证RetultCode");
			Logger.verifyEquals("总支付金额大于账单总金额,不能进行支付操作.",
					saveGoodsOwnerPaymentCall.getResultMessage(),
					"验证RetultCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SaveGoodsOwnerPayment_006() {
		Logger.start(true, "线下打款，多次支付，支付总金额超过结算金额，支付失败");
		try {
			
			Logger.comment("数据准备");
			
			// 更新账单数据为未支付
			goodsOwnerSettlementBillWrapper.updateBillToNotPay(8L);
			
			goodsOwnerPaymentWrapper.deleteByMerchantId(500011795L);
			
			Logger.comment("数据准备结束");
			
			saveGoodsOwnerPaymentReq.setAmount(BigDecimal.ONE);
			saveGoodsOwnerPaymentReq.setBillId(8L);
			saveGoodsOwnerPaymentReq.setPaymentId(TestDataManager.randomStr(8));
			saveGoodsOwnerPaymentReq.setRemark("remark");
			
			saveGoodsOwnerPaymentCall.setData(saveGoodsOwnerPaymentReq);
			saveGoodsOwnerPaymentCall.callService();
			
			saveGoodsOwnerPaymentReq.setPaymentId(TestDataManager.randomStr(8));
			saveGoodsOwnerPaymentCall.callService();
			
			saveGoodsOwnerPaymentReq.setPaymentId(TestDataManager.randomStr(8));
			saveGoodsOwnerPaymentCall.callService();
			
			saveGoodsOwnerPaymentReq.setAmount(new BigDecimal("97.01"));
			saveGoodsOwnerPaymentReq.setBillId(8L);
			saveGoodsOwnerPaymentReq.setPaymentId(TestDataManager.randomStr(8));
			saveGoodsOwnerPaymentReq.setRemark("remark");
			
			saveGoodsOwnerPaymentCall.callService();
			
			Logger.verifyEquals(3, saveGoodsOwnerPaymentCall.getRetultCode(),
					"验证RetultCode");
			Logger.verifyEquals("总支付金额大于账单总金额,不能进行支付操作.",
					saveGoodsOwnerPaymentCall.getResultMessage(),
					"验证RetultCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SaveGoodsOwnerPayment_007() {
		Logger.start(true, "线下打款，支付金额为0");
		try {
			
			Logger.comment("数据准备");
			
			// 更新账单数据为未支付
			goodsOwnerSettlementBillWrapper.updateBillToNotPay(8L);
			
			goodsOwnerPaymentWrapper.deleteByMerchantId(500011795L);
			
			Logger.comment("数据准备结束");
			
			saveGoodsOwnerPaymentReq.setAmount(BigDecimal.ZERO);
			saveGoodsOwnerPaymentReq.setBillId(8L);
			saveGoodsOwnerPaymentReq.setPaymentId(TestDataManager.randomStr(8));
			saveGoodsOwnerPaymentReq.setRemark("remark");
			
			saveGoodsOwnerPaymentCall.setData(saveGoodsOwnerPaymentReq);
			saveGoodsOwnerPaymentCall.callService();

			Logger.verifyEquals(3, saveGoodsOwnerPaymentCall.getRetultCode(),
					"验证RetultCode");
			Logger.verifyEquals("总支付金额大于账单总金额,不能进行支付操作.",
					saveGoodsOwnerPaymentCall.getResultMessage(),
					"验证RetultCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SaveGoodsOwnerPayment_008() {
		Logger.start(true, "线下打款，支付金额为负数");
		try {
			
			Logger.comment("数据准备");
			
			// 更新账单数据为未支付
			goodsOwnerSettlementBillWrapper.updateBillToNotPay(8L);
			
			goodsOwnerPaymentWrapper.deleteByMerchantId(500011795L);
			
			Logger.comment("数据准备结束");
			
			saveGoodsOwnerPaymentReq.setAmount(BigDecimal.TEN.negate());
			saveGoodsOwnerPaymentReq.setBillId(8L);
			saveGoodsOwnerPaymentReq.setPaymentId(TestDataManager.randomStr(8));
			saveGoodsOwnerPaymentReq.setRemark("remark");
			
			saveGoodsOwnerPaymentCall.setData(saveGoodsOwnerPaymentReq);
			saveGoodsOwnerPaymentCall.callService();

			Logger.verifyEquals(2, saveGoodsOwnerPaymentCall.getRetultCode(),
					"验证RetultCode");
			Logger.verifyEquals("amount:支付金额必须大于零",
					saveGoodsOwnerPaymentCall.getResultMessage(),
					"验证RetultCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}